package gamecomponents;

import java.io.Serializable;
import java.util.Date;

/**
 * Represents a time a player finished a maze with. 
 * Used by the leaderboard.
 * @author Team Deer
 */
public class Time implements Comparable<Time>, Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private final Date date;
	
	private final int hours;
	private final int minutes;
	private final int seconds;
	private final int milliseconds;
	
	// Constructor
	
	/**
	 * Constructs a new Time.
	 */
	public Time(Timer timer) {
		this.date = new Date();
		this.hours = timer.getHours();
		this.minutes = timer.getMinutes();
		this.seconds = timer.getSeconds();
		this.milliseconds = timer.getMilliSeconds();
	}

	// Methods
	
	public int getHours() {
		return this.hours;
	}

	public int getMinutes() {
		return this.minutes;
	}

	public int getSeconds() {
		return this.seconds;
	}

	public int getMilliSeconds() {
		return this.milliseconds;
	}
	
	// Methods
	
	/**
	 * Compares everything, including date etc.
	 */
	@Override
	public int compareTo(Time oTime) {
		int comp = compareTime(oTime);
		if (comp == 0)
			return date.compareTo(oTime.date);
		return comp;
	}

	/**
	 * Compares time.
	 * @param oTime The time to compare with.
	 * @return 
	 */
	private int compareTime(Time oTime){
		return (oTime.hours * 3600000 + oTime.minutes * 60000 + oTime.seconds
				* 1000 + oTime.milliseconds)
				- (this.hours * 3600000 + this.minutes * 60000 + this.seconds
						* 1000 + this.milliseconds);
	}

	/**
	 * Returns a string rep. of the date
	 * @return the date
	 */
	public String dateToString() {
		return this.date.toString();
	}

}
